package com.webull.information.center.carwler.job;

import java.net.InetAddress;
import java.util.List;
import java.util.Optional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.webull.information.center.carwler.common.model.CollectRecord;
import com.webull.information.center.carwler.common.model.parm.CollectParm;
import com.webull.information.center.carwler.dao.CollectRecordDao;
import com.webull.information.center.carwler.task.News_TickerHtmlAfreshPatch_Task;

/**
 * 新闻 多次更新机制
 * 
 * @author shimingjun
 * @date 2016年8月18日 下午1:43:45
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class News_TickerHtmlAfreshPatch_Job implements SimpleJob {
	protected final Logger logger = LogManager.getLogger(getClass());
	private @Autowired News_TickerHtmlAfreshPatch_Task fullTickerNewsHtmlPatchTask;
	private @Autowired CollectRecordDao collectRecordDao;
	private final String collectJob = "fullTickerNewsHtmlPatchJob";
	private final Integer loop = 500;// 循环20次退出,获取 20*100



	@Override
	public void execute(ShardingContext context) {
		int sharding = Optional.ofNullable(context).map(v -> v.getShardingItem()).orElse(-1);
		int totalCount = Optional.ofNullable(context).map(v -> v.getShardingTotalCount()).orElse(-1);

		logger.error("FullTickerNewsHtmlPatchJob sharding：{},totalCount：{} ", sharding, totalCount);
		try {
			List<CollectRecord> records = collectRecordDao.search(collectParm());
			/*
			 * 默认从一百万开始
			 */
			long minxxx = 1000000l;
			Long lastId = Optional.ofNullable(getLastTickeId(records)).map(la -> la.longValue()).filter(v -> v > minxxx)
					.orElse(minxxx);

			logger.error("FullTickerNewsHtmlPatchJob shardExchanges start collectJob:{} ; lastId{}", collectJob,
					lastId);
			Long lastId_ = fullTickerNewsHtmlPatchTask.fullTickerNewsCaterpillar(lastId, loop);
			CollectRecord record = backRecord();
			record.setLastTickerId(Optional.ofNullable(lastId_).map(las -> las.intValue()).orElse(null));
			Integer collId = collectRecordDao.add(record);
			logger.error("FullTickerNewsHtmlPatchJob shardExchanges end collectJob{} ,collId{}", collectJob, collId);
		} catch (Exception e) {
			logger.error("FullTickerNewsHtmlPatchJob shardExchanges collectJob div error:", e);
		}


	}


	private Integer getLastTickeId(List<CollectRecord> records) {
		if (records == null || records.isEmpty()) {
			return -1;
		} else {
			return records.get(0).getLastTickerId();
		}
	}

	private CollectParm collectParm() {
		CollectParm parm = new CollectParm();
		parm.setCollectJob(collectJob);
		parm.setRecordType(CollectRecord.RecordTypeConstants.NEWS_PATCH);
		return parm;
	}

	private CollectRecord backRecord() {
		CollectRecord record = new CollectRecord();
		record.setCollectJob(collectJob);
		record.setRecordType(CollectRecord.RecordTypeConstants.NEWS_PATCH);
		try {
			record.setCustomer(InetAddress.getLocalHost().getHostAddress());
		} catch (Exception e) {
		}
		return record;
	}

}
